package com.un.ebs.businessObject.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.enums.DbTypeEnum;
import com.un.ebs.core.metadata.EnumInfo;
import com.un.ebs.core.util.PackageUtil;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.DataSource;
import com.un.ebs.businessObject.service.DataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author aikun
 * @ClassName DataSourceController
 * @Description DataSourceController
 * @Date 2020/1/8
 */
@RestController
@RequestMapping("/datasource")
@Api(value = "sys-datasource", description = "数据源管理")
public class DataSourceController {
    @Autowired
    DataSourceService dataSourceService;

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "获取数据源列表分页")
    public WebResult<IPage<DataSource>> list(@RequestParam(required = false) Integer dbType,
                                             @RequestParam(required = false) String userName,
                                             @RequestParam(required = false) String name,
                                             @RequestParam int pageSize,
                                             @RequestParam int pageIndex) {
        return WebUtils.success(dataSourceService.list(dbType, userName, name, pageSize, pageIndex));
    }

    @GetMapping("getDbTypeEnums")
    @ApiOperation(value = "getDbTypeEnums", notes = "获取数据源类型")
    public WebResult<List<EnumInfo>> getDbTypeEnums() {
        return WebUtils.success(PackageUtil.getEnums(DbTypeEnum.class));
    }

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "新增数据源")
    public WebResult add(@Valid @RequestBody DataSource dataSource) {
        dataSourceService.add(dataSource);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改数据源")
    public WebResult update(@Valid @RequestBody DataSource dataSource) {
        dataSourceService.update(dataSource);
        return WebUtils.success();
    }

    @GetMapping("getById")
    @ApiOperation(value = "getById", notes = "id获取数据源对象")
    public WebResult<DataSource> getById(@RequestParam String id) {
        return WebUtils.success(dataSourceService.getById(id));
    }

    @PostMapping("delete")
    @ApiOperation(value = "delete", notes = "删除数据源")
    public WebResult delete(@RequestParam String id) {
        dataSourceService.delete(id);
        return WebUtils.success();
    }

}
